package com.shrct.businessprocess.organization.impl;

import com.google.common.base.Objects;
import com.shrct.businessprocess.organization.RCBusinessProcessorAssignment;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeUser;
import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.organization.RCOrganizationService;
import com.shrct.businessprocess.organization.RCRoleAssignment;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.organization.events.RCEmployeeEventsManager;
import com.shrct.businessprocess.organization.impl.RCEmployeeImpl;
import com.shrct.businessprocess.organization.impl.RCEmployeeUserImpl;
import com.shrct.businessprocess.organization.impl.RCEnterpriseImpl;
import com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl;
import com.shrct.businessprocess.organization.impl.RCSupplierImpl;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.organization.RCOrganizationService")
public class RCOrganizationServiceImpl extends BaseService implements RCOrganizationService {
  @Autowired
  private RCEmployeeEventsManager employeeEventsManager;
  
  public RCEmployee registerEmployee(final String eid) {RCEmployee rCEmployee=RCEmployeeImpl.create(getObjectFactory());
    final Procedure1<RCEmployee> _function = new Procedure1<RCEmployee>() {
        public void apply(final RCEmployee employee) {
          employee.setEid(eid);RCEmployeeUser rCEmployeeUser=RCEmployeeUserImpl.create(getObjectFactory());
          final Procedure1<RCEmployeeUser> _function = new Procedure1<RCEmployeeUser>() {
              public void apply(final RCEmployeeUser it) {
                it.setUserid(eid);
                it.setPasswd(eid);
                it.setEmployee(employee);
              }
            };
          ObjectExtensions.<RCEmployeeUser>operator_doubleArrow(rCEmployeeUser, _function);
        }
      };
    RCEmployee _doubleArrow = ObjectExtensions.<RCEmployee>operator_doubleArrow(rCEmployee, _function);
    final Procedure1<RCEmployee> _function_1 = new Procedure1<RCEmployee>() {
        public void apply(final RCEmployee it) {
          RCOrganizationServiceImpl.this.employeeEventsManager.fireCreated(it);
        }
      };
    RCEmployee _doubleArrow_1 = ObjectExtensions.<RCEmployee>operator_doubleArrow(_doubleArrow, _function_1);
    return _doubleArrow_1;
  }
  
  public void registerRoleAssignment(final String roleid, final RCEmployee employee) {RCRoleAssignment rCRoleAssignment=RCRoleAssignmentImpl.create(getObjectFactory());
    final Procedure1<RCRoleAssignment> _function = new Procedure1<RCRoleAssignment>() {
        public void apply(final RCRoleAssignment it) {
          it.setRoleType(roleid);
          it.setEmployee(employee);
        }
      };
    ObjectExtensions.<RCRoleAssignment>operator_doubleArrow(rCRoleAssignment, _function);
  }
  
  public void removeRoleAssignment(final String roleId, final RCEmployee employee) {
    IGenericQuery<RCRoleAssignment> query=getObjectFactory().createGenericQuery(RCRoleAssignment.class,"select ra from com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl as ra   where (((ra.roleType) = (:roleId)) and ((ra.employee) = (:employee))) and  ra.active = 1      ").setParameter("roleId",roleId).setParameter("employee",employee);
    Collection<RCRoleAssignment> _list = query.list();
    final Procedure1<RCRoleAssignment> _function = new Procedure1<RCRoleAssignment>() {
        public void apply(final RCRoleAssignment it) {getObjectFactory().delete(it);
          
        }
      };
    IterableExtensions.<RCRoleAssignment>forEach(_list, _function);
  }
  
  public Collection<RCRoleAssignment> getRoleAssignment(final String roleId) {
    IGenericQuery<RCRoleAssignment> query=getObjectFactory().createGenericQuery(RCRoleAssignment.class,"select ra from com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl as ra   where (((ra.roleType) = (:roleId)) and ((ra.employee.isLeave) = (false))) and  ra.active = 1      ").setParameter("roleId",roleId);
    Collection<RCRoleAssignment> _list = query.list();
    return _list;
  }
  
  public Collection<RCRoleAssignment> getRoleAssignment(final RCEmployee employee) {
    IGenericQuery<RCRoleAssignment> query=getObjectFactory().createGenericQuery(RCRoleAssignment.class,"select ra from com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl as ra   where ((ra.employee) = (:employee)) and  ra.active = 1      ").setParameter("employee",employee);
    Collection<RCRoleAssignment> _list = query.list();
    return _list;
  }
  
  public RCEmployee getBusinessMan4Saler(final RCEmployee saler) {
    IGenericQuery<RCBusinessProcessorAssignment> query=getObjectFactory().createGenericQuery(RCBusinessProcessorAssignment.class,"select ba from com.shrct.businessprocess.organization.impl.RCBusinessProcessorAssignmentImpl as ba   where ((ba.saler) = (:saler)) and  ba.active = 1      ").setParameter("saler",saler);
    RCBusinessProcessorAssignment _uniqueResult = query.uniqueResult();
    RCEmployee _businessMan = _uniqueResult==null?(RCEmployee)null:_uniqueResult.getBusinessMan();
    return _businessMan;
  }
  
  public RCEnterprise registerEnterprise(final String code) {RCEnterprise rCEnterprise=RCEnterpriseImpl.create(null,getObjectFactory());
    final Procedure1<RCEnterprise> _function = new Procedure1<RCEnterprise>() {
        public void apply(final RCEnterprise it) {
          it.setCode(code);
        }
      };
    RCEnterprise _doubleArrow = ObjectExtensions.<RCEnterprise>operator_doubleArrow(rCEnterprise, _function);
    return _doubleArrow;
  }
  
  public RCSupplier registerSupplier(final String code) {RCSupplier rCSupplier=RCSupplierImpl.create(getObjectFactory());
    final Procedure1<RCSupplier> _function = new Procedure1<RCSupplier>() {
        public void apply(final RCSupplier it) {
          it.setCode(code);
        }
      };
    RCSupplier _doubleArrow = ObjectExtensions.<RCSupplier>operator_doubleArrow(rCSupplier, _function);
    return _doubleArrow;
  }
  
  public RCEnterprise getHostEnterprise() {
    RCEnterprise _xblockexpression = null;
    {
      IGenericQuery<RCEnterprise> query=getObjectFactory().createGenericQuery(RCEnterprise.class,"select e from com.shrct.businessprocess.organization.impl.RCEnterpriseImpl as e   where ((e.code) = ( '0000001' )) and  e.active = 1      ");
      RCEnterprise result = query.uniqueResult();
      RCEnterprise _xifexpression = null;
      boolean _equals = Objects.equal(result, null);
      if (_equals) {
        RuntimeException _runtimeException = new RuntimeException("Root Enterprise not initialized");
        throw _runtimeException;
      } else {
        _xifexpression = result;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public RCEnterprise findEnterpriseByCode(final String code) {
    IGenericQuery<RCEnterprise> query=getObjectFactory().createGenericQuery(RCEnterprise.class,"select e from com.shrct.businessprocess.organization.impl.RCEnterpriseImpl as e   where ((e.code) = (:code)) and  e.active = 1      ").setParameter("code",code);
    RCEnterprise _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
}
